#include "GetImgTask.h"

GetImgTask::GetImgTask(char* body, int client_fd, int len, int shmid, int msgid):BaseTask(body, client_fd, len, shmid, msgid)
{
}

void GetImgTask::working()
{
	GETIMG_REQ req = { 0 };
	memcpy(&req, body, sizeof(GETVIDEO_REQ));
	cout << "------------------------操作日志------------------" << endl;
	// 获取当前时间点（系统时钟）
	auto now = std::chrono::system_clock::now();
	auto in_time_t = std::chrono::system_clock::to_time_t(now);
	std::stringstream ss;
	ss << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d %H:%M:%S");
	cout << "时间: " << ss.str();  // ctime自带换行
	cout << "功能：获取图片列表" << endl;
	cout << "类型：接收" << endl;
	cout << "用户id：" << req.user_id << endl;
	cout << "接收数据包：" << endl;
	cout << "当前页数:" << req.imgpage << "\n"
		<< "请求图片个数:" << req.num << endl;

	//写入文件
	LogManager& log_fd = LogManager::getInstance();
	ofstream& fd = log_fd.getLogStream();
	fd << "时间: " << ss.str() << "\n"  // ctime自带换行
		<< "功能：获取图片列表\n"
		<< "类型：接收\n"
		<< "用户id：" << req.user_id << "\n"
		<< "接收数据包：\n"
		<< "当前页数:" << req.imgpage << "\n"
		<< "请求图片个数:" << req.num << "\n"
		<< "--------------------------------------------" << "\n\n";
	// 4. 强制刷新缓冲区（确保日志立即写入文件）
	fd.flush();

	//查询图片
	vector<Img> video_vector = model->img_list(req.user_id, req.num, req.imgpage);
	if (req.imgpage == 1)
	{
		int nums = model->getImgNum(req.user_id);
		img_nums[req.user_id] = nums;
	}
	if (video_vector.size() > 0)
	{

		send(req.user_id,Success, "获取图片列表:"+ to_string(video_nums[req.user_id]), img_nums[req.user_id], video_vector);
	}
	else
	{
		send(req.user_id,Failure, "获取图片列表:获取失败", 0, video_vector);
	}
}

void GetImgTask::send(int userId,int statu, string message, int num, vector<Img>& img_vector)
{
	ShmControl* shm = (ShmControl*)shmat(shmid, NULL, 0);
	int index = -1;//空闲下标
	// 寻找下一个空闲块（状态0）
	for (int i = 0; i < MEMORYNUMBER; i++) {
		int status = shm->blocks[i].status;
		if (status == 0) { // 空闲
			index = i;
			break;
		}
	}
	//获取信息并解析
	HEAD head = { 0 };
	GETIMG_RESP resp = { 0 };//响应
	char send[MEMORYSIZE] = { 0 };
	strcpy(resp.message, message.c_str());
	resp.status_code = statu;
	resp.arr_nums = img_vector.size();
	resp.img_nums = num;
	for (size_t i = 0; i < img_vector.size(); ++i)
	{
		resp.img_arr[i] = img_vector[i]; // 将 vector 中的元素复制到数组中
	}
	cout << "获取视频总数" << img_vector.size() << endl;
	for (int i = 0; i < img_vector.size(); i++)
	{
		cout << "path =" << resp.img_arr[i].img_path << endl;
	}
	//写头
	head.service_type = GETIMG;
	head.checksum = 100;
	head.data_len = sizeof(GETIMG_RESP);
	//头体结合
	memcpy(send, &head, sizeof(HEAD));
	memcpy(send + sizeof(HEAD), &resp, sizeof(GETIMG_RESP));
	//写入共享内存
	sem_p(sem_shm, 0);//获取共享内存锁
	ShmBlock* block = &shm->blocks[index];
	memcpy(block->data, send, sizeof(block->data));//考入共享内存
	block->status = 1;                   // 标记为已写
	shmdt(shm);
	sem_v(sem_shm, 0);//释放共享内存锁
	//写入消息队列
	MSG msg = { 0 };
	msg.client_fd = client_fd;
	msg.index = index;
	msg.mtype = 1;
	msgsnd(msgid, &msg, sizeof(MSG), 0);
	cout << "------------------------操作日志------------------" << endl;
	// 获取当前时间点（系统时钟）
	auto now = std::chrono::system_clock::now();
	auto in_time_t = std::chrono::system_clock::to_time_t(now);
	std::stringstream ss;
	ss << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d %H:%M:%S");
	cout << "时间: " << ss.str();  // ctime自带换行
	cout << "功能：获取图片列表" << endl;
	cout << "类型：发送" << endl;
	cout << "用户id：" << userId << endl;
	cout << "发送文件数据包：" << endl;
	cout << "状态:" << statu << "\n"
		<< "返回图片个数:" << img_vector.size() << "\n"
		<< "图片总数:" << num << "\n"
		<< "情况说明:" << message << endl;

	//写入文件
	LogManager& log_fd = LogManager::getInstance();
	ofstream& fd = log_fd.getLogStream();
	fd<< "时间: " << ss.str()<<"\n" // ctime自带换行
	<< "功能：获取图片列表\n"
	<< "类型：发送\n"
	<< "用户id：" << userId << "\n"
	<< "发送文件数据包：" <<"\n"
	<< "状态:" << statu << "\n"
	<< "返回图片个数:" << img_vector.size() << "\n"
	<< "图片总数:" << num << "\n"
	<< "情况说明:" << message <<"\n"
	<< "--------------------------------------------" << "\n\n";
	// 4. 强制刷新缓冲区（确保日志立即写入文件）
	fd.flush();
}
